package com.example.api.mall.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.nosql.redis.RedisDS;
import com.example.common.core.constant.CachePreConstant;

/**
 * @name: TokenUtils
 * @description:
 * @author: xw.s
 * @date: 2021/6/3-16:07
 */
public class SmsUtils {
    private static String SUPER_SMS_CODE = "888888";  //超级短信码，可以绕过验证

    /**
     * 发送短信
     *
     * @return
     */
    public static String sendSmsCode(String phone, String smsCode) {
        String val = RandomUtil.randomNumbers(6);
        //TODO 发送短信
        RedisDS.create().getJedis().set(getKey(phone), val);
        return val;
    }

    /**
     * 验证短信验证码
     * @return
     */
    public static boolean verifySmsCode(String phone, String smsCode) {
        if (StrUtil.isBlank(phone) || StrUtil.isBlank(smsCode)){
            return false;
        }
        if (SUPER_SMS_CODE.equals(smsCode)){
            return true;
        }
        String val = RedisDS.create().getJedis().get(getKey(phone));
        if (!smsCode.equals(val)){
            return false;
        }
        return true;
    }

    private static String getKey(String phone){
        return CachePreConstant.SMS_PREFIX + phone;
    }
}
